package android.kryten.service.impl;

import java.io.IOException;
import java.net.URLEncoder;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import android.kryten.service.TopicService;
import android.util.Log;

public class TopicServiceImpl implements TopicService{

	private static final String TAG = "TOPIC_SERVICE";
	private static String URL_CREATE_TOPIC = "opensocial/ui/topics/post";
	
	public HttpClient client;
	
	public TopicServiceImpl(){
		super();
		this.client = new DefaultHttpClient();
	}
	
	public boolean createTopic(String name, String type, String description,
			boolean isPublic) {
		
		try {
		
		String fakeAuthor = "1";
		
		String isPublicString = "0";
		if(isPublic) {
			isPublicString = "1";
		}
		
		
		HttpPost req = new HttpPost(URLEncoder.encode("http://10.0.2.2:8080/" + URL_CREATE_TOPIC + "?name=" + name + "&topicType=" + type + "&description=" + description + "&author=" + fakeAuthor + "&public=" + isPublicString,"utf-8"));
          
		
        HttpResponse resp = client.execute(req);
        if (resp.getStatusLine().getStatusCode() != 200) {
            Log.e(TAG, "Bad server response code:"+resp.getStatusLine().getStatusCode());
            //there we can return null, or throw an exception, because nothing better cannot be done            
        }
        
		} catch (Exception e) {
            Log.e(TAG, "Unable to create topic", e);
            return false;
        }
        
		return true;
	}

}
